package com.android.base.base;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.base.R;
import com.android.base.base.view.Animation;
import com.android.base.base.view.PageStyle;
import com.android.base.base.view.status.StatusBarUtil;
import com.android.base.view.header.BaseHeaderView;

import butterknife.ButterKnife;

public abstract class BaseTitleActivity extends PermissionActivity implements PageStyle,Animation{

    private BaseHeaderView mTitleHeaderBar;
    private ViewGroup mContentViewContainer;
    //导航栏
    private View headerView = null;
    //不需要bar
    protected final int STYLE_0 = 0;
    //需要bar
    protected final int STYLE_1 = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContentView();
        initStatusBar();
    }
    /**
     * 统一样式statusbar
     * */
    private void initStatusBar(){
        StatusBarUtil.setColor(this, Color.WHITE,0);
    }
    protected void setStatusBar(int color,int statusBarAlpha){
        StatusBarUtil.setColor(this, color,statusBarAlpha);
    }
    /**
     * 初始化头部导航栏
     * */
    private void initHeaderBarView(){
        headerView = LayoutInflater.from(this).inflate(getHeaderBar(), null);
        mTitleHeaderBar = (BaseHeaderView) headerView.findViewById(R.id.ly_header_bar_title);
        if(mTitleHeaderBar != null){
            initBaseTitleHeaderBar();
            initHeaderTitle();
            starHeaderBarAnim();
        }
        initHeaderView();
        //添加头部导航
        if(headerView != null){
            mContentViewContainer.addView(headerView);
        }
    }
    /**
     * 初始化统一bar
     * */
    private void initBaseTitleHeaderBar(){
        if(enableDefaultBack()){
            //使用默认的返回按钮
            mTitleHeaderBar.setHeaderLeftText(getString(R.string.text_back));
            mTitleHeaderBar.setHeaderDrawableLeft(getResources().
                    getDrawable(R.mipmap.common_back));
            mTitleHeaderBar.setHeaderLeftClicklistener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    returnBack();
                }
            });
        }
    }
    /**
     * 头部导航动画
     * 通过重写覆盖动画
     * */
    @Override
    public void starHeaderBarAnim() {
        //默认动画
    }
    /**
     * 头部导航动画结束
     * */
    @Override
    public void endHeaderBarAnim() {

    }

    /**
     * 初始化视图
     * */
    private void initContentView(){

        if(pageStyle() == STYLE_1){
            setContentView(R.layout.activity_base_title);
            mContentViewContainer = (ViewGroup) findViewById(R.id.activity_base_title);
            initHeaderBarView();
        }else{
            mContentViewContainer = (ViewGroup) findViewById(android.R.id.content);
        }
        mContentViewContainer.setBackgroundResource(getMainBackgroundColor());
//        mContentViewContainer.setBackgroundColor(getMainBackgroundColor());
        mContentViewContainer.addView(getLayoutView(),new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public int getHeaderBar() {
        return R.layout.base_titlebar;
    }

    @Override
    public int getMainBackgroundColor() {
        return R.color.app_background_main;
    }

    /**
     * 住视图布局view
     * */
    private View getLayoutView(){
        return LayoutInflater.from(this).inflate(getLayoutId(), null);
    }

    private void initHeaderView(){
        initHeaderTitle();
    }

    public BaseHeaderView getBaseHeaderView() {
        return mTitleHeaderBar;
    }

    /**
     *
     */
    protected abstract void initHeaderTitle();

    /**
     * 是否使用默认的返回处理
     */
    protected boolean enableDefaultBack() {
        return true;
    }

    /**
     * 设置mTitleHeaderBar背景
     */
    protected void setBaseHeaderViewBackgroundColor(int color) {
        if (headerView == null) {
            return;
        }
        headerView.setBackgroundColor(color);
    }

    /**
     * 设置左边文本
     */
    protected void setBaseHeaderLeftTitle(String str) {
        if (mTitleHeaderBar == null || null == str) {
            return;
        }
        mTitleHeaderBar.setHeaderLeftText(str);
    }

    /**
     * 设置标题
     */
    protected void setHeaderTitle(String title) {
        if (mTitleHeaderBar == null || null == title) {
            new Throwable("not the title");
            return;
        }
        mTitleHeaderBar.setHeaderTitle(title);
    }

    /**
     * 设置右边文本
     */
    protected void setBaseHeaderRightTitle(String str) {
        if (mTitleHeaderBar == null || null == str) {
            return;
        }
        mTitleHeaderBar.setHeaderRightTitle(str);
    }

    public TextView getBaseHeaderRightView() {
        if (mTitleHeaderBar != null) {
            return mTitleHeaderBar.getHeaderRightView();
        }
        return null;
    }

    /**
     * 设置左边图标
     */
    protected void setBaseHeaderLeftDrawable(Drawable drawable) {
        if (mTitleHeaderBar == null) {
            return;
        }
        mTitleHeaderBar.setHeaderDrawableLeft(drawable);
    }

    /**
     * 设置右边图标
     */
    protected void setBaseHeaderRightDrawable(Drawable drawable) {
        if (mTitleHeaderBar == null) {
            return;
        }
        mTitleHeaderBar.setHeaderRightDrawable(drawable);
    }

    /**
     * 设置右边点击监听
     */
    public void setOnClickHeaderRightListener(View.OnClickListener mOnClickListener) {
        mTitleHeaderBar.setHeaderRightClicklistener(mOnClickListener);
    }

    /**
     * 设置左边点击监听
     */
    public void setOnClickHeaderLeftListener(View.OnClickListener mOnClickListener) {
        mTitleHeaderBar.setHeaderLeftClicklistener(mOnClickListener);
    }
}
